<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款人" prop="payee">
        <el-input
          v-model="queryParams.payee"
          placeholder="请输入收款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款时间" prop="receiptDate">
        <el-date-picker clearable
                        v-model="queryParams.receiptDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择收款时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:receipts:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:receipts:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:receipts:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:receipts:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiptsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="财务科目类别" align="center" prop="category">
        <template slot-scope="scope">
          <span class="rectangle-box">{{ getTransformedLabel(scope.row.category) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount"/>
      <el-table-column label="收款人" align="center" prop="payee"/>
      <el-table-column label="收款时间" align="center" prop="receiptDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiptDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:receipts:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:receipts:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务收入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="财务收入科目类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择">
            <el-option
              v-for="option in this.listChamberLable"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="收款人" prop="payee">
          <el-input v-model="form.payee" placeholder="请输入收款人"/>
        </el-form-item>
        <el-form-item label="收款详情">
          <editor v-model="form.details" :min-height="192"/>
        </el-form-item>
        <el-form-item label="收款时间" prop="receiptDate">
          <el-date-picker clearable
                          v-model="form.receiptDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择收款时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div class="statistics2">收入统计：<span class="amount2">￥{{ getTotalAmount() }}</span></div>
  </div>

</template>

<style>
.rectangle-box {
  display: inline-block;
  padding: 4px 8px;
  background: #eefced;
  border: 1px solid #ddfadc;
  border-radius: 4px;
  color: #61f563;
}
.statistics2 {
  position: fixed;
  bottom: 10px;
  right: 10px;
  font-weight: bold;
}

.amount2 {
  color: green;
}
</style>
<script>
import { listReceipts, getReceipts, delReceipts, addReceipts, updateReceipts } from '@/api/system/receipts'
import { listChamberLable2 } from '@/api/system/groups'

export default {
  name: 'Receipts',
  data() {
    return {
      listChamberLable: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务收入表格数据
      receiptsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        category: null,
        amount: null,
        payee: null,
        receiptDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        category: [
          { required: true, message: '财务科目类别不能为空', trigger: 'change' }
        ],
        amount: [
          { required: true, message: '金额不能为空', trigger: 'blur' }
        ],
        payee: [
          { required: true, message: '收款人不能为空', trigger: 'blur' }
        ],
        receiptDate: [
          { required: true, message: '收款时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    listChamberLable2('1').then(response => {
      this.listChamberLable = response.data.rows.map(function(item) {
        return { label: item.label, value: item.value }
      })
    })
    this.getList()
    this.$eventBus.$on('callMethod3', this.getT);
  },
  beforeDestroy() {
    this.$eventBus.$off('callMethod3', this.getT);
  },
  mounted() {
  },

  /* computed: {
    receiptsList() {
      debugger
      return this.listChamberLable.map(item => ({ category: item.label }))
    }
  }, */
  methods: {
    getT(){
      listChamberLable2('1').then(response => {
        this.listChamberLable = response.data.rows.map(function(item) {
          return { label: item.label, value: item.value }
        })
      })
    },
    getTotalAmount() {

      this.$receipts=this.receiptsList.reduce((sum, payment) => sum + payment.amount, 0)
      return this.$receipts


    },
    getTransformedLabel(category){
      //return this.listChamberLable.map(item => item.label);
      let option = this.listChamberLable.find(item => item.value === category);
      return option ? option.label : '未知类别';
    },
    /** 查询财务收入列表 */
    getList() {
      this.loading = true
      listReceipts(this.queryParams).then(response => {
        this.receiptsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        category: null,
        amount: null,
        payee: null,
        details: null,
        receiptDate: null,
        deptId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加财务收入'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReceipts(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改财务收入'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceipts(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addReceipts(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除财务收入编号为"' + ids + '"的数据项？').then(function() {
        return delReceipts(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/receipts/export', {
        ...this.queryParams
      }, `receipts_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
